package com.chia.multienty.core.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.chia.multienty.core.domain.dto.ProcessArgDTO;
import com.chia.multienty.core.mapper.ProcessArgMapper;
import com.chia.multienty.core.mybatis.MTLambdaWrapper;
import com.chia.multienty.core.mybatis.service.impl.KutaBaseServiceImpl;
import com.chia.multienty.core.parameter.workflow.*;
import com.chia.multienty.core.pojo.ProcessArg;
import com.chia.multienty.core.service.ProcessArgService;
import com.chia.multienty.core.util.ListUtil;
import com.github.yulichang.toolkit.MPJWrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 流程类型 服务实现类
 * </p>
 *
 * @author Multienty Auto Generator
 * @since 2024-05-31
 */
@Service
@RequiredArgsConstructor
public class ProcessArgServiceImpl extends KutaBaseServiceImpl<ProcessArgMapper, ProcessArg> implements ProcessArgService {


    @Override
    public ProcessArgDTO getDetail(ProcessArgDetailGetParameter parameter) {
        return selectJoinOne(ProcessArgDTO.class,
                        MPJWrappers.<ProcessArg>lambdaJoin().eq(ProcessArg::getArgId, parameter.getArgId()));
    }

    @Override
    public void update(ProcessArgUpdateParameter parameter) {
        ProcessArg processArg = new ProcessArg();
        BeanUtils.copyProperties(parameter, processArg);
        updateByIdTE(processArg);
    }

    @Override
    public void delete(ProcessArgDeleteParameter parameter) {
        removeByIdTE(parameter.getArgId());
    }

    @Override
    public IPage<ProcessArgDTO> getPage(ProcessArgPageGetParameter parameter) {
        return selectJoinListPage(parameter.getPageObj(), ProcessArgDTO.class,
                new MTLambdaWrapper<ProcessArg>()
                        .solveGenericParameters(parameter)
                        .eq(ProcessArg::getProcessId, parameter.getProcessId())
                        .in(!ListUtil.isEmpty(parameter.getArgIds()),
                                ProcessArg::getArgId,
                                parameter.getArgIds())
        );
    }

    @Override
    public void save(ProcessArgSaveParameter parameter) {
        ProcessArg processArg = new ProcessArg();
        BeanUtils.copyProperties(parameter, processArg);

        saveTE(processArg);
        parameter.setArgId(processArg.getArgId());
    }

    @Override
    public List<ProcessArg> getProcessArgs(Long processId, List<Long> argIds) {
        return list(
                mtLambdaWrapper()
                        .in(ProcessArg::getArgId, argIds)
                        .eq(ProcessArg::getProcessId, processId)
        );
    }
}
